//
//  LOStoreViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/10/25.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "LOStoreViewController.h"
#import "StoreTableViewCell.h"
#import "LOStoreListViewController.h"

@interface LOStoreViewController ()<UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
{
    UITableView *_tab;
    NSMutableArray *_storeArr;
    UIView *_footerView;
}
@property (strong, nonatomic) UISearchBar *searchBar;
@property (assign, nonatomic) NSInteger pageNumber;
@property (assign, nonatomic) NSInteger searchNumber;
@end

@implementation LOStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"店铺";
    _pageNumber = 1;
    _searchNumber = 1;
    _storeArr = [NSMutableArray array];
    [self downloadData:@{@"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20"}];
    [self createSearchBar];
    [self createFooterView];
    [self createTableView];
}

- (void)createSearchBar
{
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10, 80, WIDTH-20, 30)];
    _searchBar.backgroundColor = [UIColor clearColor];
    _searchBar.showsCancelButton = NO;
    _searchBar.placeholder = @"";
    _searchBar.delegate = self;
    for (UIView *subView in _searchBar.subviews) {
        if ([subView isKindOfClass:[UIView  class]]) {
            [[subView.subviews objectAtIndex:0] removeFromSuperview];
            if ([[subView.subviews objectAtIndex:0] isKindOfClass:[UITextField class]]) {
                UITextField *textField = [subView.subviews objectAtIndex:0];
                textField.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
                
                //设置默认文字颜色
                UIColor *color = [UIColor grayColor];
                [textField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"搜索商品/店铺" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:color}]];
                //修改默认的放大镜图片
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 13, 13)];
                imageView.backgroundColor = [UIColor clearColor];
                imageView.image = [UIImage imageNamed:@"LineDownSearch"];
                textField.leftView = imageView;
            }
        }
    }
    [self.view addSubview:_searchBar];
}

#pragma mark-seacherDelegate
//回车键或者键盘的return触发
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    _pageNumber = 1;
    [_storeArr removeAllObjects];
    [self downloadData:@{@"page":[NSNumber numberWithInteger:_pageNumber], @"pag_size":@"20", @"shopName":_searchBar.text}];
}

- (void)downloadData:(NSDictionary *)dic
{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(LOStoreList) Parameters:dic Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([responseObject[@"data"] count] != 0) {
            [_storeArr addObjectsFromArray:responseObject[@"data"]];
        }else{
            [self.view Message:@"暂无数据"];
        }
        
        [_tab reloadData];
        [_tab.mj_header endRefreshing];
        [_tab.mj_footer endRefreshing];
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)createFooterView
{
    _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
    _footerView.backgroundColor = RGB(232, 234, 234);
}

- (void)createTableView
{
    _tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, WIDTH, HEIGHT-120) style:(UITableViewStylePlain)];
    _tab.delegate = self;
    _tab.dataSource = self;
    _tab.showsVerticalScrollIndicator = NO;
    [_tab setTableFooterView:_footerView];
    [_tab registerNib:[UINib nibWithNibName:@"StoreTableViewCell" bundle:nil] forCellReuseIdentifier:@"StoreTableViewCell"];
    [_tab registerNib:[UINib nibWithNibName:@"LOGoodsTableViewCell" bundle:nil] forCellReuseIdentifier:@"LOGoodsTableViewCell"];
    [self.view addSubview:_tab];
    
    _tab.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNumber = 1;
        [_storeArr removeAllObjects];
        [self downloadData:@{@"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20"}];
    }];
    _tab.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pageNumber ++;
        [self downloadData:@{@"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20"}];
        
    }];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _storeArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        //店铺
        StoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellConfigureDicLOStoreList:_storeArr[indexPath.row]];
        cell.back = ^{
            NSString *str;
            str = _storeArr[indexPath.row][@"shopId"];
            NSLog(@"%@",str);
            LOStoreListViewController *detail = [[LOStoreListViewController alloc]init];
            detail.shopId = str;
            [self.navigationController pushViewController:detail animated:YES];
        };
        return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_searchBar resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
